<?php
/*
 * MiniMe - a minimalistic OStatus complient microblogging tool
 * Copyright (C) 2010, Tuomas Koski - koski.tuomas@gmail.com
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * @author     Tuomas Koski <koski.tuomas@gmail.com>
 * @copyright  Tuomas Koski
 * @license    http://www.gnu.org/licenses/lgpl.txt
 */
$basedir = dirname(__FILE__);

require_once $basedir . '/../../../../classes/mm/xml/element/builder/interface.php';
require_once $basedir . '/../../../../classes/mm/xml/element/builder/abstract.php';
require_once $basedir . '/../../../../classes/mm/xml/element/abstract.php';

require_once $basedir . '/../../../../classes/mm/atom.php';
require_once $basedir . '/../../../../classes/mm/xml/element/parent.php';
require_once $basedir . '/../../../../classes/mm/atom/element/feed.php';
require_once $basedir . '/../../../../classes/mm/atom/element/generator.php';
require_once $basedir . '/../../../../classes/mm/atom/element/id.php';
require_once $basedir . '/../../../../classes/mm/atom/element/link.php';
require_once $basedir . '/../../../../classes/mm/atom/element/title.php';
require_once $basedir . '/../../../../classes/mm/atom/element/subtitle.php';
require_once $basedir . '/../../../../classes/mm/atom/element/updated.php';
require_once $basedir . '/../../../../classes/mm/atom/element/logo.php';
require_once $basedir . '/../../../../classes/mm/atom/element/name.php';
require_once $basedir . '/../../../../classes/mm/atom/element/uri.php';
require_once $basedir . '/../../../../classes/mm/atom/element/entry.php';
require_once $basedir . '/../../../../classes/mm/atom/element/content.php';

require_once $basedir . '/../../../../classes/mm/atom/element/author.php';
require_once $basedir . '/../../../../classes/mm/atom/element/author/builder.php';

require_once $basedir . '/../../../../classes/mm/atom/element/activitystream.php';
require_once $basedir . '/../../../../classes/mm/atom/element/activitystream/subject.php';
require_once $basedir . '/../../../../classes/mm/atom/element/activitystream/objecttype.php';
require_once $basedir . '/../../../../classes/mm/atom/element/activitystream/verb.php';
require_once $basedir . '/../../../../classes/mm/atom/element/activitystream/subject/builder.php';

require_once $basedir . '/../../../../classes/mm/atom/element/activitystream/object/builder.php';
require_once $basedir . '/../../../../classes/mm/atom/element/activitystream/object.php';

require_once $basedir . '/../../../../classes/mm/atom/element/poco/displayname.php';

require_once $basedir . '/../../../../classes/mm/atom/element/entry/builder.php';

require_once $basedir . '/../../../../classes/mm/xml/element/parser.php';

require_once $basedir . '/../../../../classes/mm/atom/element/feed/builder.php';


class Mm_Atom_Element_FeedTest extends PHPUnit_Framework_TestCase {
    
    public function testParsingOfPubsubhubbubCallbackInputFeedFromBuzz() {
        $fromBuzz = '<?xml version="1.0" encoding="utf-8"?>
<feed xmlns:crosspost="http://purl.org/syndication/cross-posting" 
      xmlns="http://www.w3.org/2005/Atom" 
      xmlns:buzz="http://schemas.google.com/buzz/2010" 
      gd:kind="buzz#activityFeed" 
      xmlns:georss="http://www.georss.org/georss" 
      xmlns:media="http://search.yahoo.com/mrss/" 
      xmlns:gd="http://schemas.google.com/g/2005" 
      xmlns:poco="http://portablecontacts.net/ns/1.0" 
      xmlns:activity="http://activitystrea.ms/spec/1.0/" 
      xmlns:thr="http://purl.org/syndication/thread/1.0">
  <link href="https://www.googleapis.com/buzz/v1/activities/117625412053868084488/@public?alt=atom" 
        type="application/atom+xml" 
        rel="self"/>
  <link href="http://pubsubhubbub.appspot.com/" rel="hub"/>
  <title type="text">Google Buzz Public Feed for Tuomas Koski</title>
  <updated>2010-08-21T15:54:38.116Z</updated>
  <id>tag:google.com,2010:buzz-feed:public:posted:117625412053868084488</id>
  <generator uri="http://www.google.com/buzz">Google Buzz</generator>

  <entry gd:kind="buzz#activity">
  	<title>Let\'s test this to see what we get!</title>
  	<published>2010-08-21T15:54:37.000Z</published>
  	<updated>2010-08-21T15:54:38.116Z</updated>
  	<id>tag:google.com,2010:buzz:z13hyvtyqtqewdv4j04ccnmzsvjhfdvj50o</id>
  	<link href="http://www.google.com/buzz/117625412053868084488/R4WHZv5vyFg" type="text/html" rel="alternate"/>
  	<link href="https://www.googleapis.com/buzz/v1/activities/117625412053868084488/@self/B:z13hyvtyqtqewdv4j04ccnmzsvjhfdvj50o?alt=atom" 
  	      type="application/atom+xml" 
  	      rel="self"/>
  	<link type="application/atom+xml" 
  	      href="https://www.googleapis.com/buzz/v1/activities/117625412053868084488/@self/B:z13hyvtyqtqewdv4j04ccnmzsvjhfdvj50o/@comments?alt=atom" 
  	      thr:count="0" 
  	      thr:updated="2010-08-21T15:54:38.116Z" rel="replies"/>
  	<author>
  	  <poco:id>117625412053868084488</poco:id>
  	  <poco:photoUrl/>
  	  <name>Tuomas Koski</name>
  	  <uri>http://www.google.com/profiles/koski.tuomas</uri>
  	  <link href="http://kuva.com/1.jpg" type="image/jpeg" rel="photo"/>
  	  <activity:object-type>http://activitystrea.ms/schema/1.0/person</activity:object-type>
  	</author>
  	<content type="text/html">Let&amp;#39;s test this to see what we get!</content>
  	<activity:verb>http://activitystrea.ms/schema/1.0/post</activity:verb>
  	<activity:object>
  	  <activity:object-type>http://activitystrea.ms/schema/1.0/note</activity:object-type>
  	  <content type="html">Let&amp;#39;s test this to see what we get!</content>
  	  <buzz:original-content type="text"/>
  	  <link href="http://www.google.com/buzz/117625412053868084488/R4WHZv5vyFg" type="text/html" rel="alternate"/>
  	</activity:object>
  	<source>
  	  <activity:service>
  	    <title>Buzz</title>
  	  </activity:service>
  	</source>
  	<buzz:visibility>
  		<buzz:aclentry type="group">
  		<poco:id>tag:google.com,2010:buzz-group:@me:@public</poco:id>
  		<uri>https://www.googleapis.com/buzz/v1/people/@me/@groups/G:@me:@public?alt=atom</uri>
  		<poco:name>Public</poco:name>
  		</buzz:aclentry>
    </buzz:visibility>
    <link buzz:count="0" href="https://www.googleapis.com/buzz/v1/activities/117625412053868084488/@self/B:z13hyvtyqtqewdv4j04ccnmzsvjhfdvj50o/@liked?alt=atom" 
          type="application/poco+xml" 
          rel="http://schemas.google.com/buzz/2010#liked"/>
  </entry>
</feed>
    ';
        
        $builder = new Mm_Atom_Element_Feed_Builder($fromBuzz);
        $atomFeed = $builder->build();
        $this->assertEquals("asXML", $atomFeed->asXML());
    }
    
    public function testParsingOfPubsubhubbubCallbackInputFeedFromIdentica() {
        $fromIdentica = '<?xml version="1.0" encoding="UTF-8"?>
<feed xml:lang="en-US" xmlns="http://www.w3.org/2005/Atom" xmlns:thr="http://purl.org/syndication/thread/1.0" xmlns:georss="http://www.georss.org/georss" xmlns:activity="http://activitystrea.ms/spec/1.0/" xmlns:media="http://purl.org/syndication/atommedia" xmlns:poco="http://portablecontacts.net/spec/1.0" xmlns:ostatus="http://ostatus.org/schema/1.0" xmlns:statusnet="http://status.net/schema/api/1/">
 <generator uri="http://status.net" version="0.9.4beta2">StatusNet</generator>
 <id>http://identi.ca/api/statuses/user_timeline/209172.atom</id>
 <title>testkoski timeline</title>
 <subtitle>Updates from testkoski on Identi.ca!</subtitle>
 <logo>http://avatar.identi.ca/209172-96-20100804205939.jpeg</logo>
 <updated>2010-08-19T07:44:07+00:00</updated>
<author>
 <name>testkoski</name>
 <uri>http://identi.ca/user/209172</uri>
</author>
 <link href="http://identi.ca/testkoski" rel="alternate" type="text/html"/>
 <link href="http://identi.ca/main/sup#209172" rel="http://api.friendfeed.com/2008/03#sup" type="application/json"/>
 <link href="http://identi.ca/main/push/hub" rel="hub"/>
 <link href="http://identi.ca/main/salmon/user/209172" rel="salmon"/>
 <link href="http://identi.ca/main/salmon/user/209172" rel="http://salmon-protocol.org/ns/salmon-replies"/>
 <link href="http://identi.ca/main/salmon/user/209172" rel="http://salmon-protocol.org/ns/salmon-mention"/>
 <link href="http://identi.ca/api/statuses/user_timeline/209172.atom" rel="self" type="application/atom+xml"/>
<activity:subject>
 <activity:object-type>http://activitystrea.ms/schema/1.0/person</activity:object-type>
 <id>http://identi.ca/user/209172</id>
 <title>Tester Koski</title>
 <link rel="alternate" type="text/html" href="http://identi.ca/testkoski"/>
 <link rel="avatar" type="image/jpeg" media:width="256" media:height="256" href="http://avatar.identi.ca/209172-256-20100804205939.jpeg"/>
 <link rel="avatar" type="image/jpeg" media:width="96" media:height="96" href="http://avatar.identi.ca/209172-96-20100804205939.jpeg"/>
 <link rel="avatar" type="image/jpeg" media:width="48" media:height="48" href="http://avatar.identi.ca/209172-48-20100804205939.jpeg"/>
 <link rel="avatar" type="image/jpeg" media:width="24" media:height="24" href="http://avatar.identi.ca/209172-24-20100804205939.jpeg"/>
<poco:preferredUsername>testkoski</poco:preferredUsername>
<poco:displayName>Tester Koski</poco:displayName>
</activity:subject>
<entry>
 <title>Testkoski is testing.</title>
 <link rel="alternate" type="text/html" href="http://identi.ca/notice/46665690"/>
 <id>http://identi.ca/notice/46665690</id>
 <published>2010-08-19T07:44:07+00:00</published>
 <updated>2010-08-19T07:44:09+00:01</updated>
 <content type="html">Testkoski is testing.</content>
 <activity:verb>http://activitystrea.ms/schema/1.0/post</activity:verb>
 <activity:object-type>http://activitystrea.ms/schema/1.0/note</activity:object-type>
 <statusnet:notice_info local_id="46665690" source="web"></statusnet:notice_info>
 <link rel="ostatus:conversation" href="http://identi.ca/conversation/46308829"/>
 <georss:point>48.8601 2.3507</georss:point>
</entry>
</feed>';
        
        $builder = new Mm_Atom_Element_Feed_Builder($fromIdentica);
        $atomFeed = $builder->build();
        $this->assertEquals("http://identi.ca/api/statuses/user_timeline/209172.atom", $atomFeed->getId()->value);
        $this->assertEquals("StatusNet", $atomFeed->getGenerator()->value);
        $this->assertEquals("http://status.net", $atomFeed->getGenerator()->uri);
        $this->assertEquals("0.9.4beta2", $atomFeed->getGenerator()->version);
        $this->assertEquals("testkoski timeline", $atomFeed->getTitle()->value);
        $this->assertEquals("2010-08-19T07:44:07+00:00", $atomFeed->getUpdated()->value);
        
        // Let's test the activity:subject element
        $this->assertEquals("http://activitystrea.ms/schema/1.0/person", $atomFeed->getActivitySubject()->getObjectType()->value);
        $this->assertEquals("http://identi.ca/user/209172", $atomFeed->getActivitySubject()->getId()->value);
        $this->assertEquals("Tester Koski", $atomFeed->getActivitySubject()->getTitle()->value);
        
        $this->assertEquals("alternate", $atomFeed->getActivitySubject()->getAlternateLink()->attr[Mm_Atom_Element_Link::ATTRIBUTE_REL]);
        $this->assertEquals("http://identi.ca/testkoski", $atomFeed->getActivitySubject()->getAlternateLink()->attr[Mm_Atom_Element_Link::ATTRIBUTE_HREF]);
        $this->assertEquals("text/html", $atomFeed->getActivitySubject()->getAlternateLink()->attr[Mm_Atom_Element_Link::ATTRIBUTE_TYPE]);
        
        $this->assertEquals("avatar", $atomFeed->getActivitySubject()->getChildElement(Mm_Atom_Element_ActivityStream_Subject::ELEMENT_LINK_AVATAR)->rel);
        $this->assertEquals("image/jpeg", $atomFeed->getActivitySubject()->getChildElement(Mm_Atom_Element_ActivityStream_Subject::ELEMENT_LINK_AVATAR)->type);
        
        $this->assertEquals("avatar", $atomFeed->getActivitySubject()->getChildElement(Mm_Atom_Element_ActivityStream_Subject::ELEMENT_LINK_AVATAR_48)->rel);
        $this->assertEquals("image/jpeg", $atomFeed->getActivitySubject()->getChildElement(Mm_Atom_Element_ActivityStream_Subject::ELEMENT_LINK_AVATAR_48)->type);
        $this->assertEquals("http://avatar.identi.ca/209172-48-20100804205939.jpeg", $atomFeed->getActivitySubject()->getChildElement(Mm_Atom_Element_ActivityStream_Subject::ELEMENT_LINK_AVATAR_48)->href);
        
        $this->assertEquals("Tester Koski", $atomFeed->getActivitySubject()->getChildElement(Mm_Atom_Element_ActivityStream_Subject::ELEMENT_POCO_DISPLAYNAME)->value);
        
        $this->assertEquals("http://identi.ca/user/209172", $atomFeed->getAuthor()->getUri()->value);
        
        $this->assertEquals(true, is_array($atomFeed->getEntries()));
        foreach ($atomFeed->getEntries() as $id => $entry) {
            $this->assertEquals("http://identi.ca/notice/46665690", $id);
            $this->assertEquals("Testkoski is testing.", $entry->getTitle()->value);
            
            $this->assertEquals("2010-08-19T07:44:09+00:01", $entry->getUpdated()->value);
            
            $this->assertEquals("alternate", $entry->getAlternateLink()->rel);
            $this->assertEquals("text/html", $entry->getAlternateLink()->type);
            $this->assertEquals("http://identi.ca/notice/46665690", $entry->getAlternateLink()->href);
            
            $this->assertEquals("Testkoski is testing.", $entry->getContent()->value);
            $this->assertEquals("html", $entry->getContent()->type);
            
            $this->assertEquals("http://activitystrea.ms/schema/1.0/note", $entry->getActivityStreamObjectType()->value);
            $this->assertEquals("http://activitystrea.ms/schema/1.0/post", $entry->getActivityStreamVerb()->value);
        }
        //and build it
        $this->assertEquals("asXML", $atomFeed->asXML());
    }
    
}